package com.first.wchat.widget;

import java.net.InetAddress;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class IPListAdapter extends BaseAdapter{

	
	ArrayList<InetAddress> mData;
	LayoutInflater inflater;
	int position =-1;
	public IPListAdapter(Context ctx)
	{
		inflater = LayoutInflater.from(ctx);
		mData = new ArrayList<InetAddress>();
	}
	
	public void setData(ArrayList<InetAddress> data)
	{
		mData = data;
	}
	
	public void add(InetAddress newIP)
	{
		mData.add(newIP);
	}
	
	
	public void setHightLight(int pos)
	{
		position = pos;
	}
	public void remove(InetAddress ip)
	{
		if(mData.contains(ip))
			mData.remove(ip);
	}
	
	public void remove()
	{
		mData.clear();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(mData == null)
			return 0;
		
		return mData.size();
	}

	@Override
	public Object getItem(int index) {
		// TODO Auto-generated method stub
		return mData.get(index);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if(convertView == null)
		{
			convertView = inflater.inflate(com.first.wchat.R.layout.ip_item, null);
			holder = new Holder();
			holder.layout =  convertView;
			convertView.setTag(holder);
		}
		else
		{
			holder = (Holder) convertView.getTag();
		}

		TextView info = (TextView)holder.layout.findViewById(com.first.wchat.R.id.info);
		info.setText(mData.get(position).getHostAddress());
		
		return convertView;
	}

	
	public class Holder
	{
		public View layout;
	}
}
